package com.zxc.controller.common;

import com.zxc.controller.util.InsUtil;
import com.zxc.controller.util.ResponseUtil;
import com.zxc.model.Student;
import com.zxc.model.Teacher;
import com.zxc.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@SessionAttributes({"username","teaid","stuid"})
public class RegistController {
    private Logger LOG = LoggerFactory.getLogger(RegistController.class);

    @Resource
    private UserService userService;

    @RequestMapping("regist")
    public String regist(){
        return "regist";
    }

    @RequestMapping("teacherRegist")
    public String teacherRegist(){
        return "teacherRegist";
    }

    @RequestMapping(value = "registCheck",method = RequestMethod.POST)
    public void registCheck(@RequestParam("userid") int id, @RequestParam("userpass") String pass,
                               @RequestParam("username") String name, @RequestParam("insName") String insName, Model model, HttpServletResponse response) throws IOException {

        Student s = userService.getStuInfoById(id);
        if (s != null) {
            LOG.info("用户已存在");

            ResponseUtil.alert(response, "学号已存在请重新注册", "/regist");
            return;
        } else {
            // 插入用户
            Student student = new Student();
            student.setStuId(id);
            student.setStuPass(pass);
            student.setStuName(name);
            student.setInsId(InsUtil.insNameToId(insName));
            student.setInsName(insName);
            userService.insertStu(student);
            ResponseUtil.alert(response, "注册成功", "/login");
        }
        return;
    }

    @RequestMapping(value = "teacherRegistCheck",method = RequestMethod.POST)
    public void teacherRegistCheck(@RequestParam("userid") int id, @RequestParam("userpass") String pass,
                             @RequestParam("username") String name, @RequestParam("insName") String insName, Model model, HttpServletResponse response) throws IOException {

        Teacher t = userService.getTeaInfoById(id);
        if (t != null) {
            LOG.info("用户已存在");

            ResponseUtil.alert(response, "学号已存在请重新注册", "/regist");
            return;
        } else {
            // 插入用户
            Teacher teacher = new Teacher();
            teacher.setTeaId(id);
            teacher.setTeaPass(pass);
            teacher.setTeaName(name);
            userService.insertTeacher(teacher);
            ResponseUtil.alert(response, "注册成功", "/login");
        }
        return;
    }


}
